package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.base.response.JsonResult;
import cn.tedu.egnotice1.base.response.StatusCode;
import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    public JsonResult NoticeAdd(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("NoticeAddParam=" + noticeAddParam);
        log.debug("Title from NoticeAddParam: " + noticeAddParam.getTitle()); // 添加这行日志
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int num = noticeMapper.insertNotice(notice);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return JsonResult.ok(StatusCode.OPERATION_FAILED);
        }
    }
    @GetMapping("list")
    public JsonResult NoticeList(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery="+noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS= noticeMapper.selectByQuery( title,type);
        return JsonResult.ok(noticeListVOS);
    }
    @GetMapping("detail")
    public JsonResult NoriceDetail(Long id){
        log.debug("id="+id);
        Notice notice = noticeMapper.selectById(id);
        System.out.println("noticeDetilInfoVO="+notice);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, notice);
    }
    @PostMapping("del")
    public JsonResult NoticeDel(Long id){
        log.debug("id="+id);
        int num = noticeMapper.deleteNoticeById(id);
        if(num>0){
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    public JsonResult NoticeUpdate(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug(("noticeUpdateParam"+noticeUpdateParam));
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.updateNotice(notice);
        if(i>0){
            return new JsonResult(StatusCode.OPERATION_SUCCESS, notice);
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}














